package serverapplication;

import java.util.logging.Level;
import java.util.logging.Logger;
import static serverapplication.GlobalVariables.*;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Shuyi
 */
public class ServerApplication {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        try {
            DataBaseConnectionSource.init("dummy");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServerApplication.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        // open a thread to listens the start vote command from desktop app
        // the reason to open a new thread here is server app needs to create socket to report the already voted people to desktop app
        // one socket cannot either sending and listening. so we need another socket to do the job
        VoteStartListener voteStartListener=new VoteStartListener();
        Thread t1 = new Thread(voteStartListener);
        t1.start();

        // open a thread to listens the socket from web app
        WebSocketListener listener = new WebSocketListener();
        Thread t0 = new Thread(listener);
        t0.start();
        
        // the main thread listens the socket from desktop app
        ServerSocket server = new ServerSocket(DESKTOP_PORT);
        System.out.println("Waiting for desktop clients to connect . . . ");
        while (true) {
            Socket s = server.accept();
            System.out.println("desktop client connected.");
            // open a new thread for a desktop client
            DesktopService service = new DesktopService(s);
            Thread t = new Thread(service);
            t.start();
        }
    }
}

class WebSocketListener implements Runnable {

    public WebSocketListener() {
    }

    @Override
    public void run() {
        ServerSocket server=null;
        try {
            server = new ServerSocket(WEB_PORT);
        } catch (IOException ex) {
            Logger.getLogger(WebSocketListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Waiting for web clients to connect . . . ");
        while (true) {
            Socket s = null;
            try {
                s = server.accept();
            } catch (IOException ex) {
                Logger.getLogger(WebSocketListener.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("web client connected.");
            // open a new thread for a web client
            WebService service = new WebService(s);
            Thread t = new Thread(service);
            t.start();
        }
    }
}

class VoteStartListener implements Runnable {

    public VoteStartListener() {
    }

    @Override
    public void run() {
        ServerSocket server = null;
        try {
            server = new ServerSocket(START_VOTE_PORT);
        } catch (IOException ex) {
            Logger.getLogger(VoteStartListener.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Wait for a question to start vote . . . ");
        while (true) {
                        Socket s = null;
            try {
                s = server.accept();
            } catch (IOException ex) {
                Logger.getLogger(WebSocketListener.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("a question start voting");
            
        }
    }
}